<?php
namespace app\common\logic;

use think\Session;

use app\common\component\user\UserService;
use app\common\component\user\UserValidate;
use app\common\component\user_vip\UserVipService;

class UserVip {
	public function isVip() {
		$is_vip = Session::get('is_vip');
		if($is_vip) {
			return $is_vip;
		} else {
			$resultUserVip = (new UserVipService) -> getUserVip(Session::get('user_id'));
			if($resultUserVip['code'] == 0) {
				return !!$resultUserVip['data']['user_vip'] -> days;
			}

			return false;
		}
	}

	public function setVip($user_id) {
		$USER_REGISTERS_GIVE_VIP = getParam('USER_REGISTERS_GIVE_VIP');

		if($USER_REGISTERS_GIVE_VIP > 0) {
			$resultUserVip = (new UserVipService) -> getUserVip($user_id);

			if($resultUserVip['data']['user_vip'] -> top_up_time <= 0) {
				$resultUserVip['data']['user_vip'] -> top_up_time = time();
				$resultUserVip['data']['user_vip'] -> level = 1;
			}

			$resultUserVip['data']['user_vip'] -> days = $USER_REGISTERS_GIVE_VIP;
			//$resultUserVip['data']['user_vip'] -> days = 35;
			//$resultUserVip['data']['user_vip'] -> days += $USER_REGISTERS_GIVE_VIP;
			if($resultUserVip['data']['user_vip'] -> save()) {
				return [
					'code' => 0,
					'message' => 'VIP奖励成功'
				];
			}
			return [
				'code' => -1,
				'message' => 'VIP奖励失败'
			];
		}

		return [
			'code' => 0,
			'message' => '无VIP奖励'
		];
	}
}
?>